;(function($) {
	$.extend($.fn, {
		reposition: function(relativeTo, pos, offset) {
			var $component = this,
				$relativeTo = (Object.prototype.toString.call(relativeTo) == "[object String]") ? $( relativeTo ) : relativeTo,
				_relativeToPosition = $relativeTo.position(),
				_relativeToMT = 0,
				_relativeToML = 0,
				_top = 0,
				_left = 0,
				move = function(top, left) {
					$component.css("top", top).css("left", left);
				},
				calculateBorderWidth = function(cmp, direction) {
					var borderDirection = 'border-' + direction + '-width';
					if(cmp.css(borderDirection)) {
//						var shiftPixels = parseInt(cmp.css(borderDirection).replace("px", ""), 10) || 0;
//						return isNaN(shiftPixels) ? 0 : shiftPixels;
						return parseInt(cmp.css(borderDirection).replace("px", ""), 10) || 0;
					}
				};

			// reset the margin of the component being repositioned
			$component.css("margin", "0");

			// calculate the margins
			if($relativeTo.css("margin-top") && $relativeTo.css("margin-top") != "auto") {
				_relativeToMT = parseInt($relativeTo.css('margin-top').replace("px", ""), 10);
			}
			if($relativeTo.css("margin-left") && $relativeTo.css("margin-left") != "auto") {
				_relativeToML = parseInt($relativeTo.css('margin-left').replace("px", ""), 10);
			}

			if(pos == null || pos == "top") {
				_top = _relativeToPosition.top + _relativeToMT - $component.height() - calculateBorderWidth($component, "bottom") - calculateBorderWidth($relativeTo, "top");
				_left = _relativeToPosition.left + _relativeToML;
			} else if(pos == "top-right") {
				_top = _relativeToPosition.top + _relativeToMT - $component.height() - calculateBorderWidth($component, "bottom") - calculateBorderWidth($relativeTo, "top");
				_left = _relativeToPosition.left + _relativeToML + $relativeTo.width() + calculateBorderWidth($relativeTo, "right") + calculateBorderWidth($component, "left");
			} else if(pos == "top-left") {
				_top = _relativeToPosition.top + _relativeToMT - $component.height() - calculateBorderWidth($component, "bottom") - calculateBorderWidth($relativeTo, "top");
				_left = _relativeToPosition.left + _relativeToML - $component.width() - calculateBorderWidth($relativeTo, "left") - calculateBorderWidth($component, "right");
			} else if(pos == "right") {
				_top = _relativeToPosition.top + _relativeToMT;
				_left = _relativeToPosition.left + _relativeToML + $relativeTo.width() + calculateBorderWidth($relativeTo, "right") + calculateBorderWidth($component, "left");
			} else if(pos == "left") {
				_top = _relativeToPosition.top + _relativeToMT;
				_left = _relativeToPosition.left + _relativeToML - $component.width() - calculateBorderWidth($relativeTo, "left") - calculateBorderWidth($component, "right");
			} else if(pos == "bottom") {
				_top = _relativeToPosition.top + _relativeToMT + $relativeTo.height() + calculateBorderWidth($relativeTo, "bottom") + calculateBorderWidth($component, "top");
				_left = _relativeToPosition.left + _relativeToML;
			} else if(pos == "bottom-right") {
				_top = _relativeToPosition.top + _relativeToMT + $relativeTo.height() + calculateBorderWidth($relativeTo, "bottom") + calculateBorderWidth($component, "top");
				_left = _relativeToPosition.left + _relativeToML + $relativeTo.width() + calculateBorderWidth($relativeTo, "right") + calculateBorderWidth($component, "left");
			} else if(pos == "bottom-left") {
				_top = _relativeToPosition.top + _relativeToMT + $relativeTo.height() + calculateBorderWidth($relativeTo, "bottom") + calculateBorderWidth($component, "top");
				_left = _relativeToPosition.left + _relativeToML - $component.width() - calculateBorderWidth($relativeTo, "left") - calculateBorderWidth($component, "right");
			}
			// apply additional offset provided by the user. 
			if(offset && offset.length > 0) {
				var _offset = offset.split(',');
				_left = _left + parseInt(_offset[0], 10);
				_top = _top + parseInt(_offset[1], 10);
			}
			move(_top, _left);
			// returning jQuery component for chainability
			return $component;
		}
	});
})(jQuery);